﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Common;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using log4net;
using System.Reflection;
using System.Data;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class ModuleButtonSetController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.ModuleButtonSet Bll = new BLL.ModuleButtonSet();
        /// <summary>
        /// 按钮权限添加
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="modelList"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/ModuleButtonSet/Add")]
        public IHttpActionResult Add(VM_ModuleButtonSet modelList)
        {
            //log.Info($"ModID:{model.ModID};ModName:{model.ModName};ModDesc:{model.ModDesc};ModParentID:{model.ModParentID};ModURL:{model.ModUrl};ModLevel:{model.ModLevel}");
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (modelList.ButtonSetList.Count > 0)
                {
                    Bll.Delete(modelList.ButtonSetList[0].UserID);
                }
                for (int i = 0; i < modelList.ButtonSetList.Count; i++)
                {
                    Model.ModuleButtonSet model = modelList.ButtonSetList[i];
                    //验证角色ID RID是否存在  
                    if (Bll.Exists(model.UserID, (long)model.MBID))
                    {
                        resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                        resultMsg.Info = "已经存在该模块ID";
                        resultMsg.Data = "";
                    }
                    else
                    {
                        Model.ModuleButtonSet Mol = new Model.ModuleButtonSet();
                        //Mol.ModID = model.ModID;
                        Mol.UserID = model.UserID;
                        Mol.MBID = model.MBID;
                        long flag = Bll.Add(Mol);
                        //if (flag > 0)
                        //{
                        //    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                        //    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                        //    resultMsg.Data = flag;
                        //}
                        //else
                        //{
                        //    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                        //    resultMsg.Info = "添加失败";
                        //    resultMsg.Data = flag;
                        //}
                    }
                }

                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "添加成功！";

            }
            catch
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = "添加失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 得到模块列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ModuleButtonSet/Sel")]
        public IHttpActionResult Sel(string UserID)
        {
            log.Info($"获取按钮权限列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = Bll.GetList(" UserID='" + UserID + "'").Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到获取按钮权限列表信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }

        // GET: api/ModuleButtonSet
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/ModuleButtonSet/5
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/ModuleButtonSet
        public void Post([FromBody]string value)
        {
        }

        // PUT: api/ModuleButtonSet/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/ModuleButtonSet/5
        public void Delete(int id)
        {
        }
    }
}
